# -*- coding: utf-8 -*-
"""
Module Description:活动相关接口
Date: 2021/6/10
Author:Peng Ming Yuan
"""
from pyshare.dal.repository_impl import CrossAlliesRepository, CrossAlliesServerRepository


def get_server_id_list(allies_no_list):
    allies_no_list = allies_no_list
    allies_list = CrossAlliesRepository.filter_by_allies_no_list(allies_no_list, 'AlliesId')
    allies_id_list = [a.allies_id for a in allies_list]
    server_list = CrossAlliesServerRepository.filter_by_allies_id_list(allies_id_list)
    server_id_list = [s.server_id for s in server_list]
    return server_id_list, allies_id_list


def get_server_id_by_allies_no_list(allies_no_list):
    """
    为每个盟国编号映射区服id
    @param allies_no_list: 同盟国编号列表
    @return allies_server_dict: {allies_no: server_id_list, ...}
    """
    # 获取allies_id_list, allies_no_id_dict
    allies_list = CrossAlliesRepository.filter_by_allies_no_list(allies_no_list, 'AlliesId', 'AlliesNo')

    allies_id_list = []
    allies_no_id_dict = {}
    for allies in allies_list:
        allies_id_list.append(allies.allies_id)
        allies_no_id_dict.update({allies.allies_no: allies.allies_id})

    # 拼接{allies_id: [server_id,..]}
    allies_id_server_dict = {}
    allies_no_server_dict = {}
    server_list = CrossAlliesServerRepository.filter_by_allies_id_list(allies_id_list)
    for server in server_list:
        server_id_list = allies_id_server_dict.get(server.allies_id, [])
        server_id_list.append(server.server_id)
        allies_id_server_dict.update({server.allies_id: server_id_list})

    for allies_no in allies_no_list:
        allies_id = allies_no_id_dict.get(allies_no)
        allies_no_server_dict.update({allies_no: allies_id_server_dict.get(allies_id)})
    return allies_no_server_dict
